// grass/pages/relatedGoods/relatedGoods.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    //页面下拉
    onPageScroll: function(e) {
        let that = this;
        const query = wx.createSelectorQuery().in(this);
        query.select("#goodsList").boundingClientRect();
        query.selectViewport().scrollOffset();
        query.exec(function(res) {
            if (res[0]) {
                if (res[0].top <= 0) {
                    that.setData({
                        position: "fixed"
                    });
                } else {
                    that.setData({
                        position: "normal"
                    });
                }
            }
        });
    },
    /**
   * 页面的初始数据
   */
    data: {
        name: "",
        //商品名字
        position: "normal",
        doList: [ {
            //cost 已购商品 common 平台商品
            name: "已购商品",
            type: "cost"
        }, {
            name: "平台商品",
            type: "common"
        } ],
        chooseClass: "cost",
        chooseGoods: [],
        page: 1,
        size: 10,
        canUse: 1
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        let that = this;
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        that.setData({
            chooseGoods: prevPage.data.chooseGoods
        });
        that.getProList();
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        that.getProList();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //获取商品
    getProList: function() {
        var that = this;
        common.request({
            endpoint: "Plantgrass/manage/pro",
            method: "post",
            header: {
                openid: wx.getStorageSync("user").openid
            },
            data: {
                do: that.data.chooseClass,
                name: that.data.name,
                page: that.data.page,
                rows: that.data.size
            },
            success: res => {
                let proList = that.data.page == 1 ? res.rows : that.data.proList.concat(res.rows);
                proList.map(x => {
                    let isFlag = false;
                    that.data.chooseGoods.map(item => {
                        if (item.id == x.id) {
                            isFlag = 1;
                        }
                    });
                    if (isFlag == 1) {
                        x.active = 1;
                    } else {
                        x.active = 0;
                    }
                });
                that.setData({
                    proList: proList,
                    page: that.data.page + 1,
                    canUse: 1
                });
            },
            fail() {
                that.setData({
                    canUse: 1
                });
            }
        });
    },
    chooseClass: common.throttle(function(e) {
        let that = this;
        if (that.data.canUse) {
            that.setData({
                chooseClass: e[0].currentTarget.dataset.type,
                page: 1,
                proList: [],
                canUse: 0
            });
            that.getProList();
        }
    }, 1e3),
    recordValue: function(e) {
        let key = e.currentTarget.dataset.name;
        let value = e.detail.value;
        this.setData({
            [key]: value
        });
    },
    /**
   * 搜索
   */
    search: common.throttle(function() {
        let that = this;
        that.setData({
            page: 1,
            proList: []
        });
        that.getProList();
    }, 1e3),
    /**
   * 选择商品
   */
    chooseGoods: function(e) {
        let that = this;
        let chooseGoods = that.data.chooseGoods;
        let idx = e.currentTarget.dataset.index;
        let chooseItem = "proList[" + idx + "].active";
        if (!e.currentTarget.dataset.item.active) {
            //选中
            that.setData({
                [chooseItem]: 1
            });
            let goods = e.currentTarget.dataset.item;
            chooseGoods.push(goods);
        } else {
            that.setData({
                [chooseItem]: 0
            });
            chooseGoods = chooseGoods.filter(item => {
                return item.id != e.currentTarget.dataset.item.id;
            });
        }
        that.setData({
            chooseGoods: chooseGoods
        });
    },
    /**
   * 点击确定
   */
    submit: function() {
        let that = this;
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
            chooseGoods: that.data.chooseGoods
        });
        wx: wx.navigateBack({
            //返回上一个页面
            delta: 1
        });
    }
});